package com.getpebble.android.framework;

import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.FrameworkState;

public class EventSender {
    public static final String TAG = EventSender.class.getSimpleName();
    private Messenger mReplyMessenger;

    EventSender() {
    }

    public void sendFrameworkState(FrameworkState state) {
        if (state == null) {
            Trace.warning(TAG, "state null");
            return;
        }
        Trace.debug(TAG, "Sending framework state");
        Message msg = Message.obtain(null, 1);
        Bundle bundle = new Bundle();
        bundle.putParcelable("state_extra", state);
        msg.setData(bundle);
        sendMessage(msg);
    }

    void setReplyMessenger(Messenger replyMessenger) {
        this.mReplyMessenger = replyMessenger;
    }

    private void sendMessage(Message msg) {
        if (this.mReplyMessenger == null) {
            Trace.error(TAG, "Failed to send message: null receiver");
            return;
        }
        try {
            this.mReplyMessenger.send(msg);
        } catch (RemoteException re) {
            Trace.error(TAG, "Failed to send message.", re);
        }
    }
}
